package com.toolmanrln.security.component;

import com.toolmanrln.common.api.ResponseData;
import com.toolmanrln.common.api.ResultCodeEnum;
import com.toolmanrln.common.util.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ToolMan.RLN
 * @since 2020-05-25 11:14
 */
@Component
public class RestfulAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException e) throws IOException {
        ResponseUtil.responseToJson(response, ResponseData.failed(ResultCodeEnum.FORBIDDEN));
    }
}
